home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / BoxMaker++ / µZak ƒ / µZakshell.cp < prev    next >
Encoding:
Text File  |  1995-01-20  |  8.5 KB  |  388 lines  |  [TEXT/KAHL]

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <QuickDraw.h>
  4. #include <OSUtils.h>
  5. #include <ToolUtils.h>
  6. #include <Menus.h>
  7. #include <Packages.h>
  8. #include <Traps.h>
  9. #include <Files.h>
  10. #include <Aliases.h>
  11. #include <AppleEvents.h>
  12. #include <GestaltEqu.h>
  13. #include <Processes.h>
  14. #include <Fonts.h>
  15. #include <OSEvents.h>
  16. #include <Resources.h>
  17. #include <Desk.h>
  18. #include <Movies.h>
  19. #include <Folders.h>
  20.  
  21. #include "boxmaker constants.h"
  22. #include "boxmaker.h"
  23. #include "preferences.cp"
  24. #include "queue.cp"
  25.  
  26. #include "µZak error messages.h"
  27. #include "µZakshell.h"
  28.  
  29. #pragma template_access public
  30.  
  31. void main();
  32.  
  33. void main()
  34. {
  35.     muZaksettings defaultSettings =
  36.     {
  37.         iShowPreferences,
  38.         false
  39.     };
  40.     StringHandle prefsFileHandle = GetString( 128);
  41.     Handle configHandle = Get1Resource( 'cnfg', 128);
  42.     const long queueLength = (configHandle == 0) ? 50 : **(long **)configHandle;
  43.     muZakshell it( *(Str255 *)*prefsFileHandle, defaultSettings, queueLength);
  44.     it.run();
  45.     //
  46.     // For now, do not free the memory allocated properly
  47.     // (quitting is faster this way).
  48.     //    ReleaseResource( (Handle)prefsFileHandle);
  49.     //    ReleaseResource( configHandle);
  50.     //
  51. }
  52.  
  53. muZakshell::muZakshell( Str255 prefsFileName,
  54.     muZaksettings &defaultsettings, unsigned long queueLength)
  55.     : boxmaker()
  56.     , muZakPrefs( prefsFileName, defaultsettings)
  57.     , alias_queue( queueLength)
  58. {
  59.     //
  60.     // Check for presence of QuickTime:
  61.     //
  62.     long response;
  63.     OSErr result = Gestalt( gestaltQuickTime, &response);
  64.     
  65.     if( (result != noErr) || (response == 0))
  66.     {
  67.         //
  68.         // QuickTime not present => display alert saying so, and exit
  69.         //
  70.         ErrorAlertQuit( (result != noErr) ? result : response, kNeedQuickTimeError);
  71.     }
  72.     result = EnterMovies();
  73.     if( result != noErr)
  74.     {
  75.         ErrorAlertQuit( result, kEnterMoviesFailedError);
  76.     }
  77.     theMovie = 0;
  78.     theAlias = 0;
  79.     SetButtons();
  80.     SetRequeueButton();
  81.  
  82.     switch( wie_van_de_drie)
  83.     {
  84.         case iShowPreferences:
  85.             ShowPreferences();
  86.             //
  87.             // For nice feedback when scores of files are dropped
  88.             // on the shell we draw the dialog immediately.
  89.             //
  90.             DrawDialog( gMainDialog);
  91.             SetPort( gMainDialog);
  92.             ValidRect( &thePort->portRect);
  93.             break;
  94.             
  95.         case  iHideOnLaunch:
  96.             //
  97.             // First item in the Applications menu is 'Hide <current app>'
  98.             //
  99.             SystemMenu( 0xBF970001);
  100.             break;
  101.  
  102.         case iNeither:
  103.             ;
  104.     }
  105. }
  106.  
  107. muZakshell::~muZakshell()
  108. {
  109.     if( theMovie != 0)
  110.     {
  111.         StopCurrentMovie();
  112.     }
  113.     //
  114.     // Should dispose of any aliases in the queue or in 'theAlias', here.
  115.     // (see the handling of 'iCancelQueue' how to do this)
  116.     //
  117.     // This is not done since we will ExitToShell real soon now, and
  118.     // this will free the memory, anyway.
  119.     //
  120. }
  121.  
  122. void muZakshell::OpenDoc( Boolean opening)
  123. {
  124.     if( opening)
  125.     {
  126.         //
  127.         // Add the movie to the list of movies to be played.
  128.         // We do not check for errors. If the queue is full,
  129.         // the item is discarded.
  130.         //
  131.         if( !isFull())
  132.         {
  133.             AliasHandle newAlias;
  134.             if( NewAliasMinimal( &theFSSpec, &newAlias) == noErr)
  135.             {
  136.                 Add( newAlias);
  137.                 updateNumber();
  138.             }
  139.         }
  140.     }
  141. }
  142.  
  143. void muZakshell::HandleDialogEvent( short itemHit, DialogPtr theDialog)
  144. {
  145.     switch( itemHit)
  146.     {
  147.         case iShowPreferences:
  148.         case iHideOnLaunch:
  149.         case iNeither:
  150.             wie_van_de_drie = itemHit;
  151.             SetButtons();
  152.             break;
  153.         
  154.         case iRequeue:
  155.             requeue_after_playing = !requeue_after_playing;
  156.             SetRequeueButton();
  157.             break;
  158.         
  159.         case iCancelCurrent:
  160.             if( theMovie != 0)
  161.             {
  162.                 StopCurrentMovie();
  163.             }
  164.             break;
  165.  
  166.         case iCancelQueue:
  167.             while( !isEmpty())
  168.             {
  169.                 AliasHandle anAlias = Remove();
  170.                 DisposeHandle( (Handle)anAlias);
  171.             }
  172.             updateNumber();
  173.             break;
  174.             
  175.         case iNameOfFile:
  176.         case iQueueLength:
  177.         default:
  178.             ;
  179.     }
  180. }
  181.  
  182. Boolean muZakshell::mayEnterFolder( Boolean opening)
  183. {
  184.     //
  185.     // it doesn't make sense to enter a folder when the alias queue is
  186.     // full, since then any movie file found will be discarded by the
  187.     // OpenDoc handler
  188.     //
  189.     return !isFull();
  190. }
  191.  
  192. Boolean muZakshell::EventloopHook()
  193. {
  194.     Boolean result = false;
  195.     if( theMovie == 0)
  196.     {
  197.         //
  198.         // Is there an item to play?
  199.         //
  200.         if( isEmpty() || (the_status == shell_is_quitting))
  201.         {
  202.             result = true;
  203.         } else {
  204.             theAlias = Remove();
  205.             updateNumber();
  206.             StartAMovie();
  207.         }
  208.     } else {
  209.         if( IsMovieDone( theMovie) || (the_status == shell_is_quitting))
  210.         {
  211.             StopCurrentMovie( false);
  212.             if( requeue_after_playing && !isFull()
  213.                     && (the_status != shell_is_quitting))
  214.             {
  215.                 Add( theAlias);
  216.                 updateNumber();
  217.             } else {
  218.                 DisposeHandle( (Handle)theAlias);
  219.             }
  220.         } else {
  221.             const long maxMilliSecToUse = 500;
  222.             MoviesTask( theMovie, maxMilliSecToUse);
  223.         }
  224.     }
  225.     return result;
  226. }
  227.  
  228. pascal OSErr MyCoverProc( Movie theMovie, RgnHandle changedRgn, long refcon)
  229. {
  230. //    #pragma unused( theMovie, changedRgn, refcon);
  231.     return noErr;
  232. }
  233.  
  234. pascal OSErr MyUncoverProc( Movie theMovie, RgnHandle changedRgn, long refcon)
  235. {
  236. //    #pragma unused( theMovie, changedRgn, refcon);
  237.     return noErr;
  238. }
  239.  
  240. void muZakshell::StartAMovie()
  241. {
  242.     //
  243.     // First resolve the Alias:
  244.     //
  245.     FSSpec theFSSpec;
  246.     Boolean wasChanged;
  247.     const OSErr result = ResolveAlias( 0L, theAlias, &theFSSpec, &wasChanged);
  248.     
  249.     if( result == noErr)
  250.     {
  251.         //
  252.         // Note: we do not have to check whether we are in the background
  253.         // when setting the cursor. The system seems to do that for us.
  254.         //
  255.         CursHandle prisma = GetCursor( watchCursor);
  256.         SetCursor( *prisma);
  257.         ReleaseResource( (Handle)prisma);
  258.         OSErr err = OpenMovieFile( &theFSSpec, &theMovieFile, fsRdPerm);
  259.         long  controllerFlags;
  260.         
  261.         if( (err != noErr) || (GetMoviesError() != noErr))
  262.         {
  263.             ErrorAlert( err, kMovieToolboxError);
  264.         }    
  265.         short theMovieResID = 0;    // want first movie in the file
  266.     
  267.         err = NewMovieFromFile( &theMovie, theMovieFile, &theMovieResID,
  268.                     (StringPtr) 0, newMovieActive, (Boolean *) 0);
  269.     
  270.         if( (err != noErr) || (GetMoviesError() != noErr))
  271.         {
  272.             ErrorAlert( err, kMovieToolboxError);
  273.         }    
  274.         //
  275.         // Disable all non-sound tracks found:
  276.         //
  277.         const long numTracks = GetMovieTrackCount( theMovie);
  278.         unsigned long numSoundTracks = 0;
  279.         //
  280.         // 941130: Thanks to Paul C. Ho of 'All Midi' fame
  281.         // I learnt to count from track 1 (instead of from zero)
  282.         //
  283.         for( int trackNo = 1; trackNo <= numTracks; trackNo++)
  284.         {
  285.             Track theTrack = GetMovieIndTrack( theMovie, trackNo);
  286.             Media theMedia = GetTrackMedia( theTrack);
  287.             OSType mediaType;
  288.             GetMediaHandlerDescription( theMedia, &mediaType, nil, nil);
  289.             //
  290.             // Note: some older sound media have mediaType == 0, but so do some
  291.             // video media => some sound media will not be disabled.
  292.             // Also, my headers aren't up to date, so MusicMediaType isn't defined.
  293.             //
  294.             const Boolean isaSoundMedia =
  295.                 (mediaType == SoundMediaType) || (mediaType == 'musi');
  296.     
  297.             if( isaSoundMedia)
  298.             {
  299.                 numSoundTracks += 1;
  300.             } else {
  301.                 SetTrackEnabled( theTrack, false);
  302.             }
  303.         }
  304.         if( numSoundTracks == 0)
  305.         {
  306.             StopCurrentMovie();
  307.         } else {
  308.             //
  309.             // Change the cover procedures (probably not needed now
  310.             // that we call 'SetMovieBox', but we keep it in to be sure)
  311.             //
  312.             SetMovieCoverProcs( theMovie, MyUncoverProc, MyCoverProc, 0);
  313.             
  314.             const Rect nullRect = {0, 0, 0, 0};
  315.             SetMovieBox( theMovie, &nullRect);
  316.             //
  317.             // Now we can preroll and play the movie
  318.             //
  319.             err = PrerollMovie( theMovie, GetMovieDuration( theMovie), GetMovieRate( theMovie));
  320.             
  321.             if( err != noErr)
  322.             {
  323.                 ErrorAlert( err, kMovieToolboxError);
  324.             }    
  325.             StartMovie( theMovie);
  326.             updateName( theFSSpec.name);
  327.         }
  328.         InitCursor();
  329.     }
  330. }
  331.  
  332. void muZakshell::StopCurrentMovie( Boolean dispose_of_alias)
  333. {
  334.     if( theMovie != 0)    // better be safe than sorry
  335.     {
  336.         CloseMovieFile( theMovieFile);
  337.         DisposeMovie( theMovie);
  338.         theMovie = 0;
  339.         updateName( "\p");
  340.         if( dispose_of_alias)
  341.         {
  342.             DisposeHandle( (Handle)theAlias);
  343.             theAlias = 0;    // just for tidyness; a _perfect_ compiler will optimize it away
  344.         }
  345.     }
  346. }
  347.  
  348. void muZakshell::SetButtons()
  349. {
  350.     for( int itemNo = iShowPreferences; itemNo <= iNeither; itemNo++)
  351.     {
  352.         short    iType;
  353.         Handle    iHandle;
  354.         Rect    iRect;
  355.         GetDItem( gMainDialog, itemNo, &iType, &iHandle, &iRect);
  356.         SetCtlValue( (ControlHandle)iHandle, (itemNo == wie_van_de_drie));    
  357.     }
  358. }
  359.  
  360. void muZakshell::SetRequeueButton()
  361. {
  362.     short    iType;
  363.     Handle    iHandle;
  364.     Rect    iRect;
  365.     GetDItem( gMainDialog, iRequeue, &iType, &iHandle, &iRect);
  366.     SetCtlValue( (ControlHandle)iHandle, requeue_after_playing);    
  367. }
  368.  
  369. void muZakshell::updateName( Str63 theName)
  370. {
  371.     short    iType;
  372.     Handle    iHandle;
  373.     Rect    iRect;
  374.     GetDItem( gMainDialog, iNameOfFile, &iType, &iHandle, &iRect);
  375.     SetIText( iHandle, theName);
  376. }
  377.  
  378. void muZakshell::updateNumber()
  379. {
  380.     short    iType;
  381.     Handle    iHandle;
  382.     Rect    iRect;
  383.     Str15 string;
  384.     NumToString( Length(), string);
  385.     GetDItem( gMainDialog, iQueueLength, &iType, &iHandle, &iRect);
  386.     SetIText( iHandle, string);
  387. }
  388.